package com.suxin.classloader;

import java.lang.reflect.Method;

/**
 * @author Tang
 * @classname MainMethodRunner
 * @description [  ]
 * @date 2023/1/5 22:32
 */
public class MainMethodRunner {

    private final String mainClassName;

    private final String[] args;

    public MainMethodRunner(String mainClassName, String[] args) {
        this.mainClassName = mainClassName;
        this.args = args;
    }

    public void run() throws Exception {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Class<?> mainClass = loader.loadClass(mainClassName);
        Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
        mainMethod.invoke(null, new Object[]{});
    }
}
